package com.situ.mymall.controller;

import com.situ.mymall.annotation.MyLog;
import com.situ.mymall.constant.RedisConstants;
import com.situ.mymall.pojo.entity.Product;
import com.situ.mymall.pojo.query.ProductQuery;
import com.situ.mymall.service.IProductService;
import com.situ.mymall.util.PageResult;
import com.situ.mymall.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;
    @Autowired
    private RedisTemplate redisTemplate;

    //展示商品
    @RequestMapping("/list")
    public PageResult list(ProductQuery productQuery){
        return productService.list(productQuery);
    }

    //添加商品
    @MyLog(module = "添加商品模块",desc = "返回列表")
    @RequestMapping("/add")
    public Result add(Product product){
        productService.add(product);

        //将Product中真正使用的图片保存到Redis中
        //redisTemplate.opsForSet().add(RedisConstants.UPLOAD_IMAGE_TO_DB,product.getMainImage());

        return Result.ok("添加成功");
    }

    //查询id
    @RequestMapping("/selectById")
    public Result selectById(Integer id){
        Product product = productService.selectById(id);
        return Result.ok(product);
    }

    //更新
    @MyLog(module = "商品更新模块",desc = "返回列表")
    @RequestMapping("/update")
    public Result update(Product product){
        productService.update(product);
        return Result.ok("更新成功");
    }

    //删除单个数据
    @MyLog(module = "单个商品删除模块",desc = "返回列表")
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        productService.deleteById(id);
        return Result.ok("删除成功");
    }

    //批量删除
    @MyLog(module = "商品批量删除模块",desc = "返回列表")
    @RequestMapping("/deleteAll")
    public Result deleteAll(Integer[] ids){
        productService.deleteAll(ids);
        return Result.ok("删除成功");
    }

}
